package com.dotstone.chipism.activity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;

import com.dotstone.chipism.R;
import com.dotstone.chipism.bean.HttpURL;
import com.dotstone.chipism.bean.ShareInfo;
import com.dotstone.chipism.util.DeviceManager;
import com.dotstone.chipism.util.SPUtils;
import com.dotstone.chipism.util.ShareManager;
import com.dotstone.chipism.view.BlurBehind;
import com.dotstone.chipism.view.OnBlurCompleteListener;
import com.dotstone.chipism.view.slidingfragmenu.BarUtils;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ShareTargetActivity extends BaseActivity {
	private LinearLayout mBackL;
	private TextView mTitleTv;
	private Button mFamilyBtn;
	private Button mFriendBtn;
	private Button mTanantBtn;
	private Button mTransferBtn;
	private TextView tv_statusBar_mian;
	private View block;

	@Override
	public void widgetClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back_layout:
			finish();
			break;
		case R.id.family_btn:
			BlurBehind.getInstance().execute(MainActivity.mainActivity,
					new OnBlurCompleteListener() {
						@Override
						public void onBlurComplete() {
							Intent intent = new Intent(
									ShareTargetActivity.this,
									ShareContentActivity.class);
							intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
							ShareManager.getInstance().setShareType(1);
							startActivity(intent);
							finish();
						}
					});
			break;
		case R.id.friend_btn:
			BlurBehind.getInstance().execute(MainActivity.mainActivity,
					new OnBlurCompleteListener() {
						@Override
						public void onBlurComplete() {
							Intent intent = new Intent(
									ShareTargetActivity.this,
									ShareContentActivity.class);
							intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
							ShareManager.getInstance().setShareType(2);
							startActivity(intent);
							finish();
						}
					});
			break;
		case R.id.tanant_btn:
			BlurBehind.getInstance().execute(MainActivity.mainActivity,
					new OnBlurCompleteListener() {
						@Override
						public void onBlurComplete() {
							Intent intent = new Intent(
									ShareTargetActivity.this,
									AuthorConfirmActivity.class);
							intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
							ShareManager.getInstance().setShareType(3);
							ShareManager.getInstance().setmScenes(DeviceManager.getInstance().getScenes());
							ShareManager.getInstance().setmDevices(DeviceManager.getInstance().getAllDevice());
							startActivity(intent);
						}
					});
			break;
		case R.id.transfer_btn:
			BlurBehind.getInstance().execute(MainActivity.mainActivity,
					new OnBlurCompleteListener() {
						@Override
						public void onBlurComplete() {
							Intent intent = new Intent(
									ShareTargetActivity.this,
									AuthorConfirmActivity.class);
							intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
							ShareManager.getInstance().setShareType(4);
							ShareManager.getInstance().setmScenes(DeviceManager.getInstance().getScenes());
							ShareManager.getInstance().setmDevices(DeviceManager.getInstance().getAllDevice());
							startActivity(intent);
							finish();
						}
					});
			break;

		default:
			break;
		}
	}
	
	Handler mHandler = new Handler(){
		public void handleMessage(Message msg){
			switch (msg.what) {
			case 0:
				
				break;

			default:
				break;
			}
		}
	};

	@Override
	public void initParms(Bundle parms) {
		// TODO Auto-generated method stub

	}

	@Override
	public View bindView() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int bindLayout() {
		// TODO Auto-generated method stub
		return R.layout.activity_share_target;
	}

	@Override
	public void initView(View view) {
		// TODO Auto-generated method stub

		mBackL = $(R.id.back_layout);
		mTitleTv = $(R.id.title_tv);
		mTitleTv.setVisibility(View.GONE);
		mFamilyBtn = $(R.id.family_btn);
		mFriendBtn = $(R.id.friend_btn);
		mTanantBtn = $(R.id.tanant_btn);
		mTransferBtn = $(R.id.transfer_btn);
		tv_statusBar_mian = $(R.id.tv_statusBar_mian);
		block = $(R.id.block);
		mBackL.setOnClickListener(this);
		mFamilyBtn.setOnClickListener(this);
		mFriendBtn.setOnClickListener(this);
		mTanantBtn.setOnClickListener(this);
		mTransferBtn.setOnClickListener(this);
		boolVersion();
		BlurBehind.getInstance().withAlpha(100)
		// .withFilterColor(Color.parseColor("#000000"))
				.setBackground(this);
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		getAllShare();
	}

	public void boolVersion() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

			Window window = getWindow();
			window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			// 透明导航栏
//			getWindow().addFlags(
//					WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
			// 改变titlebar的高度
			int statusbarHeight = BarUtils.getStatusBarHeight(this);
			RelativeLayout.LayoutParams layoutParams1 = (RelativeLayout.LayoutParams) tv_statusBar_mian
					.getLayoutParams();
			// LinearLayout.LayoutParams l =
			// (LinearLayout.LayoutParams)block.getLayoutParams();
			layoutParams1.height = statusbarHeight;
			tv_statusBar_mian.setLayoutParams(layoutParams1);
			tv_statusBar_mian.setVisibility(View.VISIBLE);

		} else {
			tv_statusBar_mian.setVisibility(View.GONE);
		}
	}

	@Override
	public void setListener() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doBusiness(Context mContext) {
		// TODO Auto-generated method stub

	}
	
	private void getAllShare(){
		RequestParams params = new RequestParams(HttpURL.GET_SHARE_INFO_URL);
		params.setAsJsonContent(true);
		JSONObject obj = new JSONObject();
		try {
			obj.put("hid", DeviceManager.getInstance().hid);
			obj.put("key", "10000");
			obj.put("requestTime", "" + System.currentTimeMillis());
			obj.put("token",
					(String) SPUtils.get(getApplicationContext(), "token", ""));
		} catch (JSONException e1) {
			e1.printStackTrace();
		}
		params.setBodyContent(obj.toString());
		x.http().post(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				Log.i("wmy", result);
				try {
					JSONObject jo = new JSONObject(result);
					String resultCode = jo.getString("resultCode");
					if (resultCode.equals("1")) {
						// 上传成功，保存到本地
						JSONArray ja = jo.getJSONArray("data");
						ShareManager.getInstance().clearShare();
						for(int i = 0;i<ja.length();i++){
							JSONObject shareInfo = ja.getJSONObject(i);
							String shareInfoId = shareInfo.getString("shoreInfoId");
							String userId = shareInfo.getString("userId");
							String shareName = shareInfo.getString("shoreName");
							String sceneIds = shareInfo.optString("sceneIds");
							String deviceIds = shareInfo.optString("deviceIds");
							int invalidHour = shareInfo.getInt("invalidHour");
							int status = shareInfo.getInt("status");
							int type = shareInfo.getInt("type");
							String hid = shareInfo.getString("hid");
							ShareInfo s = new ShareInfo(shareInfoId, userId, shareName, invalidHour, status, type, hid);
							s.setDeviceIds(deviceIds);
							s.setSceneIds(sceneIds);
							ShareManager.getInstance().addShare(s);
						}
//						Message msg = Message.obtain();
//						msg.what = 0;
//						mHandler.sendMessage(msg);
					}else{
						
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			@Override
			public void onCancelled(CancelledException arg0) {

			}

			@Override
			public void onError(Throwable arg0, boolean arg1) {

			}

			@Override
			public void onFinished() {

			}

		});
	}

}
